<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\OrderLogic;




class OrderController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if($ob===null){
            $ob=new OrderLogic();
        }
        return $ob;
    }

    //待审核订单
    public function checkOrders(Request $request){

        return $this->index(1);
    }

    //待出库订单
    public function sendGoodsOrders(){

       return  $this->index(2);
    }

    //已出库订单
    public function  chukuOrders(){

       return  $this->index(33);
    }

    //已经发货，待收货
    public function receivedGoodsOrders(){

       return  $this->index(3);
    }

    //还款中
    public function repaymentingOrders(){

       return  $this->index(4);
    }

    //逾期订单
    public function overTimeOrders(){

       return  $this->index(8);
    }

    //完结订单
    public function overOrders(){

       return  $this->index(5);
    }

    //还未填写快递号的订单
    public function emptyNumberOrders(){

        return  $this->index(100);
    }

    //
    public function excelOrders(){
        return  $this->index(200);
    }


    //分类列表
    public function index($status=-1)
    {
        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);
        $order_status=(int)request('order_status',-1);
        if($status!=-1 && $status!=100 && $status!=200)$order_status=$status;
        if($status==33)$order_status=3;
        $order_type=(int)request('order_type');
        $order_comefrom =trim(request('order_comefrom'));
        $order_id=trim(request('order_id'));
        $goods_name=trim(request('goods_name'));
        $user_phone=trim(request('user_phone'));
        $user_name=trim(request('user_name'));
        $start_time=trim(request('start_time',''));
        $end_time=trim(request('end_time',''));
        $group_id=(int)request('group_id','');

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'order_status'=>$order_status,
            'status'=>$status,
            'order_type'=>$order_type,
            'order_comefrom'=>$order_comefrom,
            'order_id'=>$order_id,
            'goods_name'=>$goods_name,
            'user_phone'=>$user_phone,
            'user_name'=>$user_name,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'group_id'=>$group_id,
        ];

         $ob= $this->getInstance();
         $arr=$ob->getOrderList($para);
         if($arr && is_string($arr)) die(json_error($arr));

//        p($arr);die;
        switch ($status){
            case 1:
                $content= view("admin.order.checkOrders",$arr) ;break;
            case 2:
                $content= view("admin.order.sendGoodsOrders",$arr) ;break;
            case 3:
                $content= view("admin.order.receivedGoodsOrders",$arr) ;break;
            case 4:
                $content= view("admin.order.repaymentingOrders",$arr) ;break;
            case 5:
                $content= view("admin.order.overOrders",$arr) ;break;
            case 8:
                $content= view("admin.order.overTimeOrders",$arr) ;break;
            case 33:
                $content= view("admin.order.chukuOrders",$arr) ;break;
            case 100:
                $content= view("admin.order.emptyNumberOrders",$arr) ;break;
            case 200:
                $content= view("admin.order.excelOrders",$arr) ;break;
            default:
                $content= view("admin.order.index",$arr) ;
        }


        $data=[
            'content'=>$content,
            'header'=>'订单列表',
        ];
        return  $this->baseList($data);
    }

    //订单详情
    public function orderShow()
    {
        $order_id=trim(request("order_id"));
        $ob= $this->getInstance();
        $data=$ob->getOrderInfo($order_id);
        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.order.orderShow",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'订单详情',
        ];
        return  $this->frameList($data);
    }

    public function orderStagesInfo(){
        $order_id=trim(request("order_id"));
        $page= (int)request('page',1);
        $per_page=(int)request('per_page',50);

        $para=[
            'order_id'=>$order_id,
            'page'=>$page,
            'per_page'=>$per_page
        ];
        $ob= $this->getInstance();
        $data=$ob->orderStagesInfo($para);
        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.order.orderStagesInfo",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'订单账单列表',
        ];
        return  $this->frameList($data);
    }

    //支付宝风控
    public function xxhf()
    {
        $order_id= trim(request("order_id"));
        $ob= $this->getInstance();
        $data=$ob->xxhf($order_id);
        $this->result($data);
    }

    //订单取消
    public function quxiao()
    {
        $order_id=trim(request("order_id"));
        $ob= $this->getInstance();
        $data=$ob->quxiao($order_id);
        $this->result($data);
    }


    // 订单审核
    public function shenhe()
    {
        $order_id=trim(request("order_id"));
        $order_status=(int)request("order_status");
        $remarks =trim(request("remarks"));

        $ob= $this->getInstance();
        $data=$ob->shenhe($order_id,$order_status,$remarks);
        $this->result($data);
    }

    //确认出库  生成分期
    public function orderConfirmDelivery()
    {
        $order_id=trim(request("order_id"));
        $group_id=(int)request("group_id");
        $ob= $this->getInstance();
        $data=$ob->orderConfirmDelivery($order_id,$group_id);
        $this->result($data);
    }


    // 订单发货
    public function fahuo()
    {
        $order_id=trim(request("order_id"));
        $exp_number=trim(request("exp_number"));//快递单号
        $remarks=trim(request("remarks"));
        $express=trim(request("express"));//快递公司

        $express || die(json_error('请填写快递公司名称'));
        $exp_number || die(json_error('快递单号不能为空'));

        $ob= $this->getInstance();
        $data=$ob->fahuo($order_id,$exp_number,$express,$remarks);
        $this->result($data);
    }


    // 补填快递订单号
    public function setNumber()
    {
        $order_id=trim(request("order_id"));
        $exp_number=trim(request("exp_number"));//快递单号
        $remarks=trim(request("remarks"));
        $express=trim(request("express"));//快递公司

        $express || die(json_error('请填写快递公司名称'));
        $exp_number || die(json_error('快递单号不能为空'));

        $ob= $this->getInstance();
        $data=$ob->seExptNumber($order_id,$exp_number,$express,$remarks);
        $this->result($data);
    }

    //订单列表导出
    public function exportOrderExcel(){
        $xxhf=(int)request('xxhf',0);
        $order_status=(int)request('order_status',-1);
        $order_type=(int)request('order_type');
        $order_comefrom =trim(request('order_comefrom'));
        $order_id=trim(request('order_id'));
        $goods_name=trim(request('goods_name'));
        $user_phone=trim(request('user_phone'));
        $user_name=trim(request('user_name'));
        $start_time=trim(request('start_time',''));
        $end_time=trim(request('end_time',''));

        $para=[
            'xxhf'=>$xxhf,
            'order_status'=>$order_status,
            'order_type'=>$order_type,
            'order_comefrom'=>$order_comefrom,
            'order_id'=>$order_id,
            'goods_name'=>$goods_name,
            'user_phone'=>$user_phone,
            'user_name'=>$user_name,
            'start_time'=>$start_time,
            'end_time'=>$end_time
        ];

        $ob= $this->getInstance();
        $ob->exportOrderExcel($para);
    }

    //分组设置
    public function setGroup(){
        if($_POST){
            $group_id=(int)request('group_id',0);
            $order_id=isset($_POST['order_id'])?$_POST['order_id']:[];
            $para=[
                'group_id'=>$group_id,
                'order_id'=>$order_id
            ];
            $ob= $this->getInstance();
            $data=$ob->setGroup($para);
            $this->result($data);
        }

    }

    //分组解除
    public function removeGroup(){
        if($_POST){
            //$group_id=0 订单的所有分组都解除
            $group_id=(int)request('group_id',0);
            $order_id=isset($_POST['order_id'])?$_POST['order_id']:[];

            $ob= $this->getInstance();
            $data=$ob->removeGroup($order_id,$group_id);
            $this->result($data);
        }
    }

    //资金清单
    public function capitalList(){
        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);
        $order_status=(int)request('order_status',-1);
        $order_type=(int)request('order_type');
        $order_comefrom =trim(request('order_comefrom'));
        $order_id=trim(request('order_id'));
        $goods_name=trim(request('goods_name'));
        $user_phone=trim(request('user_phone'));
        $user_name=trim(request('user_name'));
        $start_time=trim(request('start_time',''));
        $end_time=trim(request('end_time',''));
        $daochu=(int)request('daochu',0);

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'order_status'=>$order_status,
            'order_type'=>$order_type,
            'order_comefrom'=>$order_comefrom,
            'order_id'=>$order_id,
            'goods_name'=>$goods_name,
            'user_phone'=>$user_phone,
            'user_name'=>$user_name,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'daochu'=>$daochu
        ];

        $ob= $this->getInstance();
        $arr=$ob->capitalList($para);
        if($arr && is_string($arr)) die(json_error($arr));

        $content= view("admin.order.capitalList",$arr) ;
        $data=[
        'content'=>$content,
        'header'=>'资金方-订单清单',
        ];
        return  $this->baseList($data);
    }




























}